package com.shimao.celnet.payment.service.impl;

import com.shimao.celnet.payment.mapper.PPayDaySumMapper;
import com.shimao.celnet.payment.model.PPayDaySum;
import com.shimao.celnet.payment.model.request.CallOut_OutPaySumRequest;
import com.shimao.celnet.payment.service.PPayDaySumService;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.shimao.celnet.payment.util.MyDateUtil.getPreDate;
import static com.shimao.celnet.payment.util.MyDateUtil.getToDayYmd;

@Service
public class PPayDaySumServiceImpl extends BaseServiceImpl<PPayDaySum>  implements PPayDaySumService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = false)
    private PPayDaySumMapper pPayDaySumMapper;

    /**
     * 查询出前一天00:00:00:--24:59:59的所有支付汇总数据
     * @return
     */
    @Override
    public List<PPayDaySum> getByPage() {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(PPayDaySum.class);
        Example.Criteria criteria = example.createCriteria();

        Date preDate = getPreDate();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(preDate);    //格式化前一天
        defaultStartDate = defaultStartDate+" 00:00:00";
        String defaultEndDate = defaultStartDate.substring(0,10)+" 23:59:59";
        //00:00:00:--24:59:59
        criteria.andBetween("invoiceTime",defaultStartDate,defaultEndDate);
        List<PPayDaySum> tableList = super.getByExample(example);
        return  tableList;
    }

    /**
     * 支付日汇总
     * @return
     */
    @Override
    public List<CallOut_OutPaySumRequest> selectSumInfo(String payType,String strDate) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(PPayDaySum.class);
        Example.Criteria criteria = example.createCriteria();
        Date preDate ;
        if(strDate == null || strDate == ""){
            preDate  = new Date();
        }else{
            preDate = MyDateUtil.StrToDate(strDate,"yyyy-MM-dd") ;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(preDate);    //格式化前一天
        Map<String,String> pMap = new HashMap<String,String>();
        //支付日期
        pMap.put("createDate",defaultStartDate);
        //支付类型
        pMap.put("payType",payType);

        List<CallOut_OutPaySumRequest> tableList = pPayDaySumMapper.selectSumInfo(pMap);

        return  tableList;
    }

    /**
     * 支付日汇总明细数据
     * @return
     */
    @Override
    public List<CallOut_OutPaySumRequest> selectSumInfo1(String payType, String strDate) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(PPayDaySum.class);
        Example.Criteria criteria = example.createCriteria();
        Date preDate ;
        if(strDate == null || strDate == ""){
            preDate  = new Date();
        }else{
            preDate = MyDateUtil.StrToDate(strDate,"yyyy-MM-dd") ;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(preDate);    //格式化前一天
        Map<String,String> pMap = new HashMap<String,String>();
        //支付日期
        pMap.put("createDate",defaultStartDate);
        //支付类型
        pMap.put("payType",payType);

        List<CallOut_OutPaySumRequest> tableList = pPayDaySumMapper.selectSumInfo1(pMap);

        return  tableList;
    }

    /**
     * 调用支付数据汇总存储过程
     */
    @Override
    public void activateProcess(String strDate){
        Date preDate ;
        if(strDate == null || strDate == ""){
            preDate  = new Date();    //当前时间
        }else{
            preDate = MyDateUtil.StrToDate(strDate,"yyyy-MM-dd") ;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String startDate = sdf.format(preDate);    //格式化前一天
        pPayDaySumMapper.procPayDaySum(startDate);
    }

}
